weighted_treatment_group_demographics <- function(variable_name, df){
  supplied_variable <- sym(variable_name)
  ungrouped_df <- df %>% 
    summarise(numbernewnametotal = 
                round(sum(wt_new_W21 * !!supplied_variable, na.rm = TRUE), 0),
              proportionnewnametotal = 
                round(wtd.mean(!!supplied_variable, weights = wt_new_W21), 2),
              standarddeviationnewnametotal = 
                round(sqrt(wtd.var(!!supplied_variable, weights = wt_new_W21)), 2)) %>%
    mutate(variable := !!variable_name) 
  grouped_df <- df %>% group_by(treatment_w21) %>%
    summarise(number = round(sum(wt_new_W21 * !!supplied_variable, na.rm = TRUE), 0),
              proportion = round(wtd.mean(!!supplied_variable, weights = wt_new_W21), 2),
              standarddeviation = round(sqrt(wtd.var(!!supplied_variable, weights = wt_new_W21)), 2)) %>%
    pivot_wider(names_from = treatment_w21,
                names_sep = "newname",
                values_from = c(number, proportion, standarddeviation)) %>%
    mutate(variable := !!variable_name)
  new_df <- left_join(ungrouped_df, grouped_df) %>%
    relocate(variable,
             numbernewnametotal,
             numbernewnamecontrol,
             numbernewnamepositive,
             numbernewnamenegative,
             proportionnewnametotal,
             proportionnewnamecontrol,
             proportionnewnamepositive,
             proportionnewnamenegative,
             standarddeviationnewnametotal,
             standarddeviationnewnamecontrol,
             standarddeviationnewnamepositive,
             standarddeviationnewnamenegative)
  return(new_df)
}

weighted_means_dvars <- function(variable_name){
  supplied_variable <- sym(variable_name)
  ungrouped_df <- ex %>% 
    summarise(meannewnametotal = 
                round(wtd.mean(!!supplied_variable, weights = wt_new_W21), 2),
              standarddeviationnewnametotal = 
                round(sqrt(wtd.var(!!supplied_variable, weights = wt_new_W21)), 2)) %>%
    mutate(variable := !!variable_name) 
  grouped_df <- ex %>% group_by(treatment_w21) %>%
    summarise(mean = round(wtd.mean(!!supplied_variable, weights = wt_new_W21), 2),
              standarddeviation = round(sqrt(wtd.var(!!supplied_variable, weights = wt_new_W21)), 2)) %>%
    pivot_wider(names_from = treatment_w21,
                names_sep = "newname",
                values_from = c(mean, standarddeviation)) %>%
    mutate(variable := !!variable_name)
  new_df <- left_join(ungrouped_df, grouped_df) %>%
    relocate(variable,
             meannewnametotal,
             meannewnamecontrol,
             meannewnamepositive,
             meannewnamenegative,
             standarddeviationnewnametotal,
             standarddeviationnewnamecontrol,
             standarddeviationnewnamepositive,
             standarddeviationnewnamenegative)
  return(new_df)
}

dvar_dont_knows <- function(answered_name, data){
  
  supplied_answered <- sym(answered_name)
 
  ungrouped_df <- data %>% 
    summarise(numbernewnametotal = round(sum(!!supplied_answered, na.rm = TRUE), 0),
              proportionnewnametotal = 
                round(mean(!!supplied_answered, na.rm = TRUE), 2)) %>%
    mutate(answered := !!answered_name) 

  grouped_df <- data %>% group_by(treatment_w21) %>%
    summarise(number = round(sum(!!supplied_answered, na.rm = TRUE), 0),
              proportion = round(mean(!!supplied_answered, na.rm = TRUE), 2)) %>%
    pivot_wider(names_from = treatment_w21,
                names_sep = "newname",
                values_from = c(number, proportion)) %>%
    mutate(answered := !!answered_name) 
  
  new_df <- left_join(ungrouped_df, grouped_df) %>%
    relocate(answered,
             numbernewnametotal,
             numbernewnamecontrol,
             numbernewnamepositive,
             numbernewnamenegative,
             proportionnewnametotal,
             proportionnewnamecontrol,
             proportionnewnamepositive,
             proportionnewnamenegative)

  return(new_df)
}

min_max_dvars <- function(variable_name){
  
  supplied_variable <- sym(variable_name)
 
  ungrouped_df <- ex %>% 
    summarise(minnewnametotal = 
                min(!!supplied_variable, na.rm = TRUE),
              maxnewnametotal = 
                max(!!supplied_variable, na.rm = TRUE)) %>%
    mutate(variable := !!variable_name) 

  grouped_df <- ex %>% group_by(treatment_w21) %>%
    summarise(min = min(!!supplied_variable, na.rm = TRUE),
              max = max(!!supplied_variable, na.rm = TRUE)) %>%
    pivot_wider(names_from = treatment_w21,
                names_sep = "newname",
                values_from = c(min, max)) %>%
    mutate(variable := !!variable_name) 
  
  new_df <- left_join(ungrouped_df, grouped_df) %>%
    relocate(variable,
             minnewnametotal,
             minnewnamecontrol,
             minnewnamepositive,
             minnewnamenegative,
             maxnewnametotal,
             maxnewnamecontrol,
             maxnewnamepositive,
             maxnewnamenegative)

  return(new_df)
}
